package cn.zhiwen.stream;

import cn.zhiwen.entity.Person;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

import static java.util.stream.Collectors.toList;

public class StreamSort {
    public static void main(String[] args) {
        List<Person> javaProgrammers = new ArrayList<Person>() {
            {
                add(new Person("Elsdond", "Jaycob", "Java programmer", "male", 43, 2000));
                add(new Person("Camsens", "Brittany", "Java programmer", "female", 23, 1500));
                add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800));
                add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));
                add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));
                add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));
                add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));
                add(new Person("Bayden", "Corrina", "Java programmer", "female", 35, 1700));
                add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2000));
                add(new Person("Addison", "Pam", "Java programmer", "female", 34, 1300));
            }
        };
        System.out.println("根据 name 排序,并显示前5个 Java programmers:");
        Comparator<Person> comparator = (e, p)->(e.getFirstName().compareTo(p.getFirstName()));
        javaProgrammers.stream().sorted(comparator).limit(5).collect(toList()).forEach((p)-> System.out.println(p.getFirstName()));

        System.out.println("根据 salary 排序 Java programmers:");
        javaProgrammers.stream().sorted((p1,p2)->(p1.getSalary()-p2.getSalary()));
    }
}
